package com.example.jbmsuppliers.sinopec.utlis;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * 退出程序的管理类，使用单利模式
 * 保证只有一个这样的实例存在
 * Created by 家帮忙 on 2017/1/9.
 */

public class ExitAppUtlis {
    private static ExitAppUtlis exitAppUtlis;

    private ExitAppUtlis() {
    }

    public static ExitAppUtlis getInstance() {
        if (exitAppUtlis == null) {
            synchronized (ExitAppUtlis.class) {
                if (exitAppUtlis == null)
                    exitAppUtlis = new ExitAppUtlis();
            }
        }
        return exitAppUtlis;
    }

    /*
    * 1.创建Activity存放的集合
    * **/
    private List<Activity> MActivityList = new ArrayList<>();

    /**
     * 1.添加每一个activity需要是oncreate()方法中调用
     */
    public void addActivity(Activity activity) {
        MActivityList.add(activity);
    }

    /**
     * 1.移除Activtiy在onDestroy()方法中调用
     */
    public void deActivity(Activity activity) {
        MActivityList.remove(activity);
    }

    /**
     * 退出程序的方法
     */
    public void exit() {
        for (Activity activity : MActivityList) {
            activity.finish();

        }
        System.exit(0);
    }
}
